﻿using Net.Sz;
using Net.Sz.Framework.Netty;
using Net.Sz.Framework.Netty.Buffer;
using Net.Sz.Framework.Netty.Tcp;
using Net.Sz.Framework.Szlog;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SocketClient
{

    class Program
    {

        private static SzLogger log = SzLogger.getLogger();

        static void Main(string[] args)
        {

            /*创建客户端连接的处理handler程序*/
            NettyPool.ClientSessionHandler = new ClientHandler();
            /*客户端创建和服务器的连接*/
            NettyClient clientA = new NettyClient(ip: "127.0.0.1", port: 9527);
            /*连接服务器*/
            clientA.Connect();
            clientA.WriteAndFlush(0, new byte[2]);
            if (log.IsInfoEnabled())
                log.Info("我是客户端");
            while (clientA.Connected)
            {
            }
            log.Debug(clientA.Connected);
            //Console.WriteLine(md5("101,1540992192823,8a072daec865bc26c1651437fe8228fee0a7027e,ca7abf40ae7fde77658b92113468d6a3"));
            Console.ReadLine();
        }

        static string md5(string str)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] strs = System.Text.Encoding.UTF8.GetBytes(str);
            byte[] retVal = md5.ComputeHash(strs);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 客户端连接的处理handler程序
        /// </summary>
        class ClientHandler : IIOSessionHandler
        {

            public void ChannelActive(IOSession session)
            {

            }

            public void ExceptionCaught(IOSession session, Exception ex)
            {

            }

            public void ChannelRead(IOSession session, int msgId, byte[] buffer)
            {
                /*收到服务器发送来的消息*/

                if (msgId == 2)
                {

                    /*封装服务器发来的消息，读取内容*/
                    BufferReader br = new BufferReader(buffer);
                    /*读取服务器发送的long 类型数据*/
                    long mmid = br.ReadInt64();

                    /*收到消息回复消息处理*/
                    log.Error("收到服务器消息：" + session.ID + "  消息唯一回复id" + mmid);

                }
            }

            public void ChannelUnregistered(IOSession session, params string[] obj)
            {

            }

            public void ChannelInactive(IOSession session)
            {
            }
        }
    }
}
